﻿Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics


Namespace ReactionGame
    Partial Public Class Program
        Dim font As Font = Resources.GetFont(Resources.FontResources.NinaB)
        Dim score As Integer = 0
        Dim correct As Boolean = True
        Dim random As New Random
        Dim randValue As Integer
        Dim WithEvents timer As GT.Timer = New GT.Timer(1000)  ' once every 1000 milliseconds
        Dim WithEvents joyTimer As GT.Timer = New GT.Timer(100) ' once every N milliseconds


        ' This is run when the mainboard is powered up or reset. 
        Public Sub ProgramStarted()
            '*******************************************************************************************
            ' Hardware modules added in the Program.gadgeteer designer view are used by typing 
            ' their name followed by a period, e.g.  button.  or  camera.
            '
            ' Many hardware modules generate useful events. To set up actions for those events, use the 
            ' left dropdown box at the top of this code editing window to choose a hardware module, then
            ' use the right dropdown box to choose the event - an event handler will be auto-generated.
            '*******************************************************************************************/

            ' Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started")

            font = Resources.GetFont(Resources.FontResources.NinaB)
            ' Display four blocks on each side of the screen and a message in 
            ' the middle saying click to start
            display.SimpleGraphics.DisplayRectangle(GT.Color.Blue, 1, GT.Color.Blue, 0, 50, 20, 60)
            display.SimpleGraphics.DisplayRectangle(GT.Color.Red, 1, GT.Color.Red, 40, 0, 60, 20)
            display.SimpleGraphics.DisplayRectangle(GT.Color.Green, 1, GT.Color.Green, 40, 140, 60, 20)
            display.SimpleGraphics.DisplayRectangle(GT.Color.Yellow, 1, GT.Color.Yellow, 110, 50, 20, 60)

            display.SimpleGraphics.DisplayTextInRectangle("Ready...", 40, 50, 60, 40, GT.Color.White, font)

            Thread.Sleep(2000)
            timer.Start()
            Debug.Print("Game started")

        End Sub

        ' If you want to do something periodically, declare a GT.Timer by uncommenting the below line
        '   and then use the dropdown boxes at the top of this window to generate a Tick event handler.
        ' Dim WithEvents timer As GT.Timer = new GT.Timer(1000)  ' every second (1000ms)

        Private Sub showRectangle(number As Integer)
            Select Case number
                Case 1
                    display.SimpleGraphics.DisplayRectangle(GT.Color.Blue, 1, GT.Color.Blue, 0, 50, 20, 60)
                Case 2
                    display.SimpleGraphics.DisplayRectangle(GT.Color.Red, 1, GT.Color.Red, 40, 0, 60, 20)
                Case 3
                    display.SimpleGraphics.DisplayRectangle(GT.Color.Green, 1, GT.Color.Green, 40, 140, 60, 20)
                Case 4
                    display.SimpleGraphics.DisplayRectangle(GT.Color.Yellow, 1, GT.Color.Yellow, 110, 50, 20, 60)
            End Select
        End Sub

        Private Function detectJoystickMovement(number As Integer) As Boolean
            ' function to return true or false depending on position of joystick.
            Dim x As Double = joystick.GetPosition.X
            Dim y As Double = joystick.GetPosition.Y
            Select Case number ' random colour chosen by number
                Case 1 ' blue chosen - low x value
                    If x < -0.5 Then
                        Return True
                    Else
                        Return False
                    End If

                Case 2 ' red chosen - low y value
                    If y > 0.5 Then
                        Return True
                    Else
                        Return False

                    End If
                Case 3  ' green chosen - high y value
                    If y < -0.5 Then
                        Return True
                    Else
                        Return False
                    End If
                Case 4 ' yellow chosen - large X value
                    If x > 0.5 Then
                        Return True
                    Else
                        Return False
                    End If
            End Select

            Return False
        End Function



        Private Sub timer_Tick(timer As Gadgeteer.Timer) Handles timer.Tick

            Dim message As String
            ' should be true first time and then indicate whether last block was successful
            joyTimer.Stop() ' stop the other timer
            display.SimpleGraphics.Clear()
            If Not correct Then ' didn't get last one
                Debug.Print("Timed out")
                message = "GAME OVER!"
                display.SimpleGraphics.DisplayText(message, font, GT.Color.White, 25, 40)
                message = "Score " & score.ToString()
                display.SimpleGraphics.DisplayText(message, font, GT.Color.White, 40, 60)
                timer.Stop()
            Else
                ' select the next random block to show
                display.SimpleGraphics.DisplayText("Score " + score.ToString(), font, GT.Color.White, 40, 60)
                randValue = random.Next(4) + 1
                showRectangle(randValue)
                correct = False
                joyTimer.Start()
            End If

        End Sub

        Private Sub joyTimer_Tick(timer As Gadgeteer.Timer) Handles joyTimer.Tick
            joyTimer.Stop()
            ' keep checking to see if they have moved the joystick
            correct = detectJoystickMovement(randValue)
            If correct Then ' has moved joystick in right direction
                score += 1
            Else
                joyTimer.Start()
            End If

        End Sub

         
    End Class
End Namespace